package com.usefullc.crawler.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.jayway.jsonpath.JsonPath;
import com.usefullc.common.enums.YesNoEnum;
import com.usefullc.common.util.secure.Base64;
import com.usefullc.crawler.domain.ActiveInstance;
import com.usefullc.crawler.param.CrawlerTaskSubmitParam;
import com.usefullc.crawler.service.ActiveInstanceService;
import com.usefullc.crawler.service.CrawlerTaskService;
import com.usefullc.crawler.vo.CrawlerTaskVo;
import com.usefullc.system.controller.BaseController;
import com.usefullc.system.controller.WebResultBuilder;
import com.usefullc.system.entity.WebResult;
import com.usefullc.system.validator.UsefulValid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.minidev.json.JSONArray;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;

import static com.usefullc.system.controller.WebResultBuilder.successResult;

/**
 * 爬虫任务控制类
 * @author: 星空
 * @date: 2022-12-15
 */
@RestController
@RequestMapping("/crawler/crawlerTask/")
@Api( tags="爬虫任务")
public class CrawlerTaskController extends BaseController {


    @Autowired
    private CrawlerTaskService crawlerTaskService;

    @Autowired
    private ActiveInstanceService activeInstanceService;

    private CrawlerTaskVo list(){
        CrawlerTaskVo crawlerTaskVo = new CrawlerTaskVo();
        crawlerTaskVo.setStorePageSource(YesNoEnum.NO.getCode());
        crawlerTaskVo.setEnableImage(YesNoEnum.NO.getCode());
        crawlerTaskVo.setEnableJavascript(YesNoEnum.NO.getCode());
        crawlerTaskVo.setEnableStyle(YesNoEnum.NO.getCode());
        crawlerTaskVo.setEnableProxy(YesNoEnum.NO.getCode());
        crawlerTaskVo.setTimeRdMs("");
        crawlerTaskVo.setActiveInstanceId(0L);
        crawlerTaskVo.setActiveId(0L);
        crawlerTaskVo.setCookie("");
        crawlerTaskVo.setStartTime(0L);
        crawlerTaskVo.setAnalyseBanExpress("");
        crawlerTaskVo.setAnalyseExpress("");
        crawlerTaskVo.setAnalyseDataExpress("" +
                "var nodeList = responseJsonData['aweme_list'];\n" +
                "var ccUrls = [];\n" +
                "for(var i = 0; i < nodeList.length; i++){\n" +
                "   var node = nodeList[i];\n" +
                "   var ccUrl = 'https://www.douyin.com/video/'+node['aweme_id'];\n" +
                "   ccUrls.push(ccUrl);\n" +
                "}\n" +
                "return ccUrls;");
//        crawlerTaskVo.setSwitchProxyExpress("");
//        crawlerTaskVo.setUrl("https://www.douyin.com/user/MS4wLjABAAAAGLwNfNCA8cwEhJTEqlXlvKFYVrVYwUKcF3sAr11jHs0");
        crawlerTaskVo.setUrl("https://www.douyin.com/user/MS4wLjABAAAAEOGva6OAXe9oKwuaaZDDOU4EBheqAcZnx1q6P-Rga_Y");
//        crawlerTaskVo.setUrl("https://www.douyin.com/user/MS4wLjABAAAAwDMkx23mPXdKUUa1rR3gaiI7qR4Yn9FuVYn6j_LDAuE");
        crawlerTaskVo.setExtractDataUrl("https://www.douyin.com/aweme/v1/web/aweme/post/");
//        crawlerTaskVo.setUiEventScript("");
//        crawlerTaskVo.setWaitCssSelectorExpress("");
        return crawlerTaskVo;
    }

    private CrawlerTaskVo detail(){
                CrawlerTaskVo crawlerTaskVo = new CrawlerTaskVo();
        crawlerTaskVo.setStorePageSource(YesNoEnum.NO.getCode());
        crawlerTaskVo.setEnableImage(YesNoEnum.NO.getCode());
        crawlerTaskVo.setEnableJavascript(YesNoEnum.NO.getCode());
        crawlerTaskVo.setEnableStyle(YesNoEnum.NO.getCode());
        crawlerTaskVo.setEnableProxy(YesNoEnum.NO.getCode());
        crawlerTaskVo.setTimeRdMs("");
        crawlerTaskVo.setActiveInstanceId(0L);
        crawlerTaskVo.setActiveId(0L);
        crawlerTaskVo.setCookie("");
        crawlerTaskVo.setStartTime(0L);
        crawlerTaskVo.setAnalyseBanExpress("");
        crawlerTaskVo.setAnalyseDataExpress("");
        crawlerTaskVo.setAnalyseExpress("var nodeList = document.querySelectorAll(\"div.leftContainer.w0R6mo9z > div.tMWlo89q > div > div > div.sX7gMtFl.comment-mainContent > div\");\n" +
                "var result = [];\n" +
                "for(var i = 0; i < nodeList.length-1; i++){  //-1是因为最后div 是加载更多，或者没有更多\n" +
                "   var node = nodeList[i];\n" +
                "   var index = i+1;\n" +
                "   var userUrl = node.querySelector(\"div:nth-child(\"+index+\") > div > div.RHiEl2d8 > div > div.AMzEzRWv.comment-item-info-wrap > div.Uvaas5kD > div > a\").href;\n" +
                "   var nickname = node.querySelector(\"div:nth-child(\"+index+\") > div > div.RHiEl2d8 > div > div.AMzEzRWv.comment-item-info-wrap > div.Uvaas5kD > div > a\").textContent;\n" +
                "   var html = node.querySelector(\"div:nth-child(\"+index+\") > div > div.V3cGkKvu.comment-item-avatar > a\").outerHTML;" +
                "   var avatar = null;//node.querySelector(\"div:nth-child(\"+index+\") > div > div.V3cGkKvu.comment-item-avatar > a > div > img\").src;\n" +
                "   var comment = node.querySelector(\"div:nth-child(\"+index+\") > div > div.RHiEl2d8 > div > div.a9uirtCT\").textContent;\n" +
                "   var area = node.querySelector(\"div:nth-child(\"+index+\") > div > div.RHiEl2d8 > div > div.L4ozKLf7\").textContent;\n" +
                "   var object = {\"userUrl\":userUrl,\"nickname\":nickname,\"avatar\":avatar,\"comment\":comment,\"area\":area,\"h\":html};\n" +
                "   result.push(object);\n" +
                "}\n" +
                "return result;");
        crawlerTaskVo.setSwitchProxyExpress("");
//        crawlerTaskVo.setUrl("https://www.douyin.com/user/MS4wLjABAAAAGLwNfNCA8cwEhJTEqlXlvKFYVrVYwUKcF3sAr11jHs0");
        crawlerTaskVo.setUrl("https://www.douyin.com/video/7242313462141947188");
//        crawlerTaskVo.setExtractDataUrl();
//        crawlerTaskVo.setUiEventScript("");
        crawlerTaskVo.setWaitCssSelectorExpress("");
        return crawlerTaskVo;
    }


    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "爬虫任务提取",response = WebResult.class)
    @GetMapping(path = "taskPull",produces = "application/json")
    public WebResult taskPull() {
        CrawlerTaskVo crawlerTaskVo = crawlerTaskService.pullCrawlerTask();
        //list
//        CrawlerTaskVo crawlerTaskVo = list();

        //detail
//        CrawlerTaskVo crawlerTaskVo1 = detail();
        return WebResultBuilder.successResult(crawlerTaskVo);
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "爬虫任务提取",response = WebResult.class)
    @GetMapping(path = "taskMultiPull",produces = "application/json")
    public WebResult taskMultiPull() {
        List<CrawlerTaskVo> crawlerTaskVos = crawlerTaskService.pullMultiCrawlerTask();
        return WebResultBuilder.successResult(crawlerTaskVos);
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "爬虫任务提取",response = WebResult.class)
    @GetMapping(path = "taskMultiPullByTaskId",produces = "application/json")
    public WebResult taskMultiPullByTaskId(@Valid @RequestParam Long taskId) {
        List<CrawlerTaskVo> crawlerTaskVos = crawlerTaskService.pullMultiCrawlerTaskByTaskId(taskId);
        return WebResultBuilder.successResult(crawlerTaskVos);
    }


    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "爬虫任务状态监测",response = WebResult.class)
    @GetMapping(path = "taskStatusCheck",produces = "application/json")
    public WebResult taskStatusCheck(@Valid @RequestParam Long id) {
        ActiveInstance activeInstance = activeInstanceService.findById(id);
        return WebResultBuilder.successResult(activeInstance.getStatus());
    }


    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "获取cookie字符",response = WebResult.class)
    @GetMapping(path = "getCookies",produces = "application/json")
    public WebResult getCookies(@Valid @RequestParam Long activeInstanceId) {
        String cookies = crawlerTaskService.getCookies(activeInstanceId);
        return WebResultBuilder.successResult(cookies);
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "获取单个代理",response = WebResult.class)
    @GetMapping(path = "getProxies",produces = "application/json")
    public WebResult getProxies(@Valid @RequestParam Long activeProcessId) {
        String proxies = crawlerTaskService.getProxies(activeProcessId);
//        String proxies = "127.0.0.1:8888";
//        String proxies = "127.0.0.1:9000";
        return WebResultBuilder.successResult(proxies);
    }




    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "爬虫任务执行结果提交", response = WebResult.class)
    @PostMapping(path = "/submit", produces = "application/json")
    @UsefulValid
    public WebResult submit(@UsefulValid(save=true) @RequestBody CrawlerTaskSubmitParam param) {
        crawlerTaskService.submitCrawlerTask(param);
        return successResult();
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "分片活动任务执行结果提交", response = WebResult.class)
    @PostMapping(path = "/sliceTaskFinishSubmit", produces = "application/json")
    @UsefulValid
    public WebResult sliceTaskFinishSubmit(@UsefulValid(save=true) @RequestBody CrawlerTaskSubmitParam param) {
        crawlerTaskService.sliceFinishSubmit(param);
        return successResult();
    }


    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "爬虫Ban提交", response = WebResult.class)
    @PostMapping(path = "/submitBan", produces = "application/json")
    @UsefulValid
    public WebResult submitBan(@UsefulValid(save=true) @RequestBody CrawlerTaskSubmitParam param) {
//        crawlerTaskService.submitCrawlerTask(param);
        logger.info("ban={}",param);
        return successResult();
    }

    public static void main(String[] args) throws IOException {
//        String str = FileUtils.readFileToString(new File("/Users/long3/workspace/usefullc/doc/crawler/douyin/hetianyu/test3.txt"), Charset.defaultCharset());
////        JSONObject jsonObject = JSON.parseObject(str);
////        System.out.println(jsonObject.toJSONString());
//
//        byte[] decode = Base64.decode(str);
////        byte[] decode = Base64.decode(param.getResponseAnalyseBody());
//        JSONObject jsonObject = new JSONObject();
//        String string = new String(decode);
//        Object obj = JSONObject.parse(string);
//        System.out.println(obj);


        String responseAnalyseBody = "[[\"abc\",\"ddd\"]]";
//        String responseAnalyseBody = "[\"abc\",\"ddd\"]";
        List<Object> analyseUrls = JsonPath.read(responseAnalyseBody, "$.[*]");
        for (Object analyseUrl : analyseUrls) {
            if(analyseUrl instanceof JSONArray){
                ((JSONArray)analyseUrl).forEach(data->{

                });
            }
            System.out.println(analyseUrl);
        }
        System.out.println(analyseUrls);


    }
}
